package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private TagMapper tagMapper;
    
    @Override
    public PageInfo<StrategyComment> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<StrategyComment> strategyComments = strategyCommentMapper.selectForList(qo);
        return new PageInfo<>(strategyComments);
    }

    @Override
    public void save(StrategyComment comment, String[] tagArr) {
        //设置作者
        comment.setUser(UserContext.getCurrentUser());
        //保存点评信息,获取id值
        strategyCommentMapper.insert(comment);
        for (String tagStr : tagArr) {
            Tag tag = new Tag();
            tag.setName(tagStr);
            //保存标签,获取id值
            tagMapper.insert(tag);
            //维护中间表
            strategyCommentMapper.insertRelation(comment.getId(),tag.getId());
        }
    }
}
